package com.timmy.chi.choosephoto;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.timmy.chi.MyApp;
import com.timmy.chi.R;
import com.timmy.chi.utils.DisplayUtils;

import java.io.IOException;

/**
 * 作者：陈冬冬
 * <p>
 * 说明：自定义裁剪图片
 * <p>
 * 时间：2015-11-3 上午10:38:26
 */
public class CropImageActivity extends Activity implements OnClickListener {

    private CropImageView mCropImage;
    private TextView tv_next_step;
    private TextView tv_last_step;
    public static final String IMGURL = "imgURL";
    private String imgURL;
    private Drawable d = null;
    private int screenWidth;
    private int screenHeight;
    private static final String TAG = "CropImageActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop_image);
        mCropImage = (CropImageView) findViewById(R.id.activity_crop_image_crop_image_view);
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_crop_image_rl);
        View view = findViewById(R.id.activity_crop_image_view);
        tv_last_step = (TextView) findViewById(R.id.activity_crop_image_tv_last_step);
        tv_next_step = (TextView) findViewById(R.id.activity_crop_image_tv_next_step);
        tv_last_step.setOnClickListener(this);
        tv_next_step.setOnClickListener(this);

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();
        LayoutParams rlParam = rl.getLayoutParams();
        rlParam.height = (screenHeight - screenWidth) / 2 - DisplayUtils.dip2px(this, 11);
        LayoutParams viewParam = view.getLayoutParams();
        viewParam.height = (screenHeight - screenWidth) / 2 - DisplayUtils.dip2px(this, 11);
        ;
        imgURL = getIntent().getStringExtra(IMGURL);
        if (imgURL != null) {

            Bitmap bitmap = BitmapFactory.decodeFile(imgURL);
            if (readPictureDegree(imgURL) != 0) {
                bitmap = toturn(bitmap, readPictureDegree(imgURL));
            }

            Drawable drawable = new BitmapDrawable(bitmap);
            mCropImage.setDrawable(drawable, screenWidth, screenWidth);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_crop_image_tv_last_step:
                finish();
                break;
            case R.id.activity_crop_image_tv_next_step:
                cropImage1();
                break;
            default:
                break;
        }
    }

    private void cropImage1() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                MyApp.getInstance().selectBitmap = mCropImage.getCropImage();
                //				Log.e(TAG, "app.selectBitmap="+app.selectBitmap);
                setResult(RESULT_OK);
                finish();
            }
        }).start();
    }

    public static void jump2CropImageActivity(Activity activity, String imgURL, int requestCode) {
        Intent intent = new Intent(activity, CropImageActivity.class);
        intent.putExtra(CropImageActivity.IMGURL, imgURL);
        activity.startActivityForResult(intent, requestCode);
        activity.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
    }


    /**
     * 读取照片exif信息中的旋转角度
     *
     * @param path 照片路径
     * @return角度
     */
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    public static Bitmap toturn(Bitmap img, int degree) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree); /*翻转90度*/
        int width = img.getWidth();
        int height = img.getHeight();
        img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
        return img;
    }
}
